<template>
    <div style="background-color: #FBFBEA;border-radius: 5px;">
        <div  style="padding-left: 20px;display: inline-block;margin-top: 20px;font-weight: bold;font-size: 20px;">个人信息</div>
    <el-form :model="userform">

      <el-form-item label="账户" disabled prop="name" label-width="15%">
        <el-input type="text" v-model="userform.name" autocomplete="off" ></el-input>
      </el-form-item>

    <el-form-item label="旧密码" prop="password3" label-width="15%">
      <el-input type="password" v-model="userform.oldpassword" autocomplete="off" show-password></el-input>
    </el-form-item>

    <el-form-item label="新密码" prop="password1" label-width="15%">
      <el-input type="password" v-model="userform.newpassword" autocomplete="off" show-password></el-input>
    </el-form-item>


    <el-form-item label="确认密码" prop="password2" label-width="15%">
      <el-input type="password" v-model="userform.comfirmpassword" autocomplete="off" show-password></el-input>
    </el-form-item>

  </el-form>
      <el-button type="primary" @click="changePasswordSubmit()">确 定</el-button>
    
 


</div>
</template>








<script>
import request from '@/request/request.js';
export default {
    data() {
        return {
          user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
            userform: {
                image: "",
                password: "",
                oldpassword: "",
          newpassword: "",
          comfirmpassword: "",
            },
            }
        },
    methods: {
        passwordEdit(){
        request.get("/user/" + this.user.id).then((res) => {
          if (res.code === "0") {
            this.userform.name = res.data.name;
            this.userform.id=res.data.id;
          }
        })
      },
      changePasswordSubmit(){
        if (!this.userform.newpassword || this.userform.newpassword !== this.userform.comfirmpassword||this.userform.oldpassword !== this.user.password){
          this.$message.error("原密码不对，两次密码输入不一致或者密码为空");}
        else{
        this.userform.password = this.userform.newpassword;
        request.put("/user/password", this.userform).then((res) => {
          if (res.code === "0") {
            this.$message.success({ message: "修改成功", type: "success" });
           
            this.userform.password = "";
            this.userform.newpassword = "";
            this.userform.comfirmpassword = "";
            this.passwordFormVisible = false;
          } else {
            this.$message({ message: "修改失败", type: "error" });
          }
        });
      }},
    },
    created() {
        this.passwordEdit();
    }
}
</script>